package com.ruoyi.js.controller;

import java.util.List;

import com.ruoyi.js.domain.JsJudge;
import com.ruoyi.js.service.IJsJudgeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.js.domain.JsPlace;
import com.ruoyi.js.service.IJsPlaceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 场地Controller
 * 
 * @author greejoy
 * @date 2024-10-16
 */
@Controller
@RequestMapping("/js/place")
public class JsPlaceController extends BaseController
{
    private String prefix = "js/place";

    @Autowired
    private IJsPlaceService jsPlaceService;

    @Autowired
    private IJsJudgeService jsJudgeService;

    @RequiresPermissions("js:place:view")
    @GetMapping()
    public String place()
    {
        return prefix + "/place";
    }

    /**
     * 查询场地列表
     */
    @RequiresPermissions("js:place:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@CookieValue("app_match_id") Long matchId, JsPlace jsPlace)
    {
        jsPlace.setMatchId(matchId);
        startPage();
        List<JsPlace> list = jsPlaceService.selectJsPlaceList(jsPlace);
        return getDataTable(list);
    }

    /**
     * 导出场地列表
     */
    @RequiresPermissions("js:place:export")
    @Log(title = "场地", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@CookieValue("app_match_id") Long matchId, JsPlace jsPlace)
    {
        jsPlace.setMatchId(matchId);
        List<JsPlace> list = jsPlaceService.selectJsPlaceList(jsPlace);
        ExcelUtil<JsPlace> util = new ExcelUtil<JsPlace>(JsPlace.class);
        return util.exportExcel(list, "place");
    }

    /**
     * 新增场地
     */
    @GetMapping("/add")
    public String add(@CookieValue("app_match_id") Long matchId, ModelMap mmap)
    {
        List<JsJudge> judgeList = jsJudgeService.selectJsJudgeList(new JsJudge(matchId));
        mmap.put("judgeList", judgeList);
        return prefix + "/add";
    }

    /**
     * 新增保存场地
     */
    @RequiresPermissions("js:place:add")
    @Log(title = "场地", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@CookieValue("app_match_id") Long matchId, JsPlace jsPlace)
    {
        jsPlace.setMatchId(matchId);
        return toAjax(jsPlaceService.insertJsPlace(jsPlace));
    }

    /**
     * 修改场地
     */
    @GetMapping("/edit/{id}")
    public String edit(@CookieValue("app_match_id") Long matchId, @PathVariable("id") Long id, ModelMap mmap)
    {
        JsPlace jsPlace = jsPlaceService.selectJsPlaceById(id);
        mmap.put("jsPlace", jsPlace);

        List<JsJudge> judgeList = jsJudgeService.selectJsJudgeList(new JsJudge(matchId));
        mmap.put("judgeList", judgeList);

        return prefix + "/edit";
    }

    /**
     * 修改保存场地
     */
    @RequiresPermissions("js:place:edit")
    @Log(title = "场地", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JsPlace jsPlace)
    {
        return toAjax(jsPlaceService.updateJsPlace(jsPlace));
    }

    /**
     * 删除场地
     */
    @RequiresPermissions("js:place:remove")
    @Log(title = "场地", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jsPlaceService.deleteJsPlaceByIds(ids));
    }
}
